import pytest, time
import datetime
from Case.python_ui.Common.basepage import Basepage
from selenium import webdriver
from Case.python_ui.PageLocate.dunning_report_locator import DunningReportLocator
from selenium.common.exceptions import NoSuchElementException


class DunningReportDetail(Basepage):

    # # 判断是否包含元素
    # def check_contain_str(self, string, except_string):
    #     if except_string in string:
    #         assert True
    #     else:
    #         assert False, '实际值:' + string + '期望值:' + except_string
    #
    # 校验某个属性值是否符合预期
    def check_attribute(self, real_result, except_result):
        if real_result == except_result:
            assert True
        else:
            assert False, '实际值:' + real_result + '期望值:' + except_result

    # # 元素是否存在
    # def element_is_exist(self, locate):
    #     doc = '判断元素是否存在'
    #     try:
    #         self.get_element(locate, doc)
    #     except NoSuchElementException as e:
    #         return False
    #     else:
    #         return True





    # 进入User Status-dunning界面
    def enter_user_status_dunning_report(self):
        time.sleep(2)
        doc = "User Status-dunning界面"
        for i in range(3):
            try:
                eles = self.get_elements(DunningReportLocator.default_page_expanded, doc)
                for ele in eles:
                    ele.click()
                    time.sleep(1)
                self.wait_elevisble(DunningReportLocator.Dashboards_tab, doc)
                self.click_element(DunningReportLocator.Dashboards_tab, doc)
                self.wait_elevisble(DunningReportLocator.Dashboards_UseStatus_title, doc)
                self.click_element(DunningReportLocator.Dashboards_UseStatus_title, doc)
                break
            except:
                self.refresh_page()
                continue
        time.sleep(5)
        self.wait_elevisble(DunningReportLocator.Dashboards_UseStatus_page, doc)

    # User Status-all_dunning界面
    def user_status_dunning_data(self):
        time.sleep(2)
        doc = "User Status-all_dunning界面"
        system_yesterday = datetime.date.today() + datetime.timedelta(-1)
        self.click_element(DunningReportLocator.Dashboards_UseStatus_title, doc)
        self.move_mouse_to_element(DunningReportLocator.UseStatus_Dunning_title)
        self.click_element(DunningReportLocator.UseStatus_Dunning_title, doc)
        time.sleep(5)
        self.check_attribute(self.get_element(DunningReportLocator.UseStatus_Dunning_the_data, doc).text, format(system_yesterday))


    # User Status_dunning_all界面
    def user_status_click_all_dunning_report(self):
        time.sleep(2)
        doc = "User Status-dunning界面"
        self.click_element(DunningReportLocator.Dashboards_UseStatus_title, doc)
        self.move_mouse_to_element(DunningReportLocator.UseStatus_Dunning_title)
        self.click_element(DunningReportLocator.UseStatus_Dunning_title, doc)
        time.sleep(5)
        self.move_mouse_to_element(DunningReportLocator.All_Dunning_title)
        self.click_element(DunningReportLocator.All_Dunning_title, doc)
        self.wait_elevisble(DunningReportLocator.All_Dunning_title, doc)
        time.sleep(5)

    # User Status_dunning_1界面
    def user_status_click_dunning_one_report(self):
        time.sleep(2)
        doc = "User Status-dunning界面"
        self.click_element(DunningReportLocator.Dashboards_UseStatus_title, doc)
        self.move_mouse_to_element(DunningReportLocator.UseStatus_Dunning_title)
        self.click_element(DunningReportLocator.UseStatus_Dunning_title, doc)
        time.sleep(5)
        self.move_mouse_to_element(DunningReportLocator.Dunning_one_title)
        self.click_element(DunningReportLocator.Dunning_one_title, doc)
        self.wait_elevisble(DunningReportLocator.Dunning_one_title, doc)
        time.sleep(5)

    # User Status_dunning_2界面
    def user_status_click_dunning_two_report(self):
        time.sleep(2)
        doc = "User Status-dunning界面"
        self.click_element(DunningReportLocator.Dashboards_UseStatus_title, doc)
        self.move_mouse_to_element(DunningReportLocator.UseStatus_Dunning_title)
        self.click_element(DunningReportLocator.UseStatus_Dunning_title, doc)
        time.sleep(5)
        self.move_mouse_to_element(DunningReportLocator.Dunning_two_title)
        self.click_element(DunningReportLocator.Dunning_two_title, doc)
        self.wait_elevisble(DunningReportLocator.Dunning_two_title, doc)
        time.sleep(5)

    # User Status_dunning_3界面
    def user_status_click_dunning_three_report(self):
        time.sleep(2)
        doc = "User Status-dunning界面"
        self.click_element(DunningReportLocator.Dashboards_UseStatus_title, doc)
        self.move_mouse_to_element(DunningReportLocator.UseStatus_Dunning_title)
        self.click_element(DunningReportLocator.UseStatus_Dunning_title, doc)
        time.sleep(5)
        self.move_mouse_to_element(DunningReportLocator.Dunning_three_title)
        self.click_element(DunningReportLocator.Dunning_three_title, doc)
        self.wait_elevisble(DunningReportLocator.Dunning_three_title, doc)
        time.sleep(5)

    # 查看User Status_dunning页面字段
    def check_user_states_dunning_pages_fields(self):
        time.sleep(2)
        doc = "查看User status- Dunning页面字段"
        self.change_windows(DunningReportLocator.Dunning_one_title)
        time.sleep(2)
        self.check_attribute(self.get_element(DunningReportLocator.Account_email_title, doc).get_attribute("textContent"), 'Account Email')
        self.check_attribute(self.get_element(DunningReportLocator.Dunning_Status, doc).get_attribute("textContent"), 'Dunning Status')
        self.check_attribute(self.get_element(DunningReportLocator.Dunning_1_Time, doc).get_attribute("textContent"), 'Dunning 1 Time')
        self.check_attribute(self.get_element(DunningReportLocator.Account_Type, doc).get_attribute("textContent"), 'Account Type')
        self.check_attribute(self.get_element(DunningReportLocator.User_Name, doc).get_attribute("textContent"), 'User Name')
        self.check_attribute(self.get_element(DunningReportLocator.Phone_Number, doc).get_attribute("textContent"), 'Phone Number')
        self.check_attribute(self.get_element(DunningReportLocator.Credit_Account, doc).get_attribute("textContent"), 'Credit Account  (Currency)')
        self.check_attribute(self.get_element(DunningReportLocator.Cash_Account, doc).get_attribute("textContent"), 'Cash Account (Currency)')
        self.check_attribute(self.get_element(DunningReportLocator.Outstanding_Amount, doc).get_attribute("textContent"), 'Outstanding Amount (Currency)')
        self.check_attribute(self.get_element(DunningReportLocator.User_Group, doc).get_attribute("textContent"), 'User Group')
        self.check_attribute(self.get_element(DunningReportLocator.Organization, doc).get_attribute("textContent"), 'Organization')
        self.check_attribute(self.get_element(DunningReportLocator.Creation, doc).get_attribute("textContent"), 'Creation Time')


        # 查看User Status_dunning数据按照时间排序
    def check_user_states_dunning_pages_time_sort(self):
        time.sleep(2)
        doc = "查看User status- Dunning页面time字段"
        self.change_windows(DunningReportLocator.Dunning_one_title)
        time.sleep(2)
        # self.check_attribute(self.get_element(DunningReportLocator.Dunning_1_Time_Data1, doc).get_attribute("textContent"), '06-12-2023 04:04:01')
        time_sort1 = self.get_element(DunningReportLocator.Dunning_1_Time_Data1, doc).get_attribute("textContent")
        time_sort11 = datetime.datetime.strptime(time_sort1, "%m-%d-%Y %H:%M:%S")  # 转换成指定格式的字符串
        print(time_sort11)
        time_sort2 = self.get_element(DunningReportLocator.Dunning_1_Time_Data2, doc).get_attribute("textContent")
        time_sort22 = datetime.datetime.strptime(time_sort2, "%m-%d-%Y %H:%M:%S")
        print(time_sort22)
        time_sort3 = self.get_element(DunningReportLocator.Dunning_1_Time_Data3, doc).get_attribute("textContent")
        time_sort33 = datetime.datetime.strptime(time_sort3, "%m-%d-%Y %H:%M:%S")
        print(time_sort33)
        if time_sort11 < time_sort22:
            assert False
        elif time_sort22 < time_sort33:
            assert False
        else:
            assert True

    # 查看User Status_dunning 1数据
    def check_user_states_dunning_one_pages_fields(self):
        time.sleep(2)
        doc = "查看User status- Dunning页面status字段"
        self.change_windows(DunningReportLocator.Dunning_one_title)
        datas = self.get_elements(DunningReportLocator.Dunning_Status_data1, doc)
        for data in datas:
            self.check_attribute(data.text, 'Dunning 1')

    # 查看User Status_dunning 2数据
    def check_user_states_dunning_two_pages_fields(self):
        time.sleep(2)
        doc = "查看User status- Dunning页面status字段"
        self.change_windows(DunningReportLocator.Dunning_two_title)
        # self.click_element(DunningReportLocator.Dunning_Status_button, doc)
        datas = self.get_elements(DunningReportLocator.Dunning_Status_data2, doc)
        for data in datas:
            self.check_attribute(data.text, 'Dunning 2')

    # 查看User Status_dunning 3数据
    def check_user_states_dunning_three_pages_fields(self):
        time.sleep(2)
        doc = "查看User status- Dunning页面status字段"
        self.change_windows(DunningReportLocator.Dunning_three_title)
        # self.click_element(DunningReportLocator.Dunning_Status_button, doc)
        datas = self.get_elements(DunningReportLocator.Dunning_Status_data3, doc)
        for data in datas:
            self.check_attribute(data.text, 'Dunning 3')

    # def check_user_states_dunning_one_pages_fields(self):
    #     time.sleep(2)
    #     doc = "查看User status- Dunning页面字段"
    #     self.change_windows(DunningReportLocator.Dunning_one_title)
    #     text = self.get_elements(DunningReportLocator.Dunning_Status_data, doc)
    #     self.click_element(DunningReportLocator.Dunning_Status_button, doc)
    #     time.sleep(2)
    #     radios = self.get_elements(DunningReportLocator.Dunning_Status_click_button, doc)
    #     count = 0
    #     for radio in radios:
    #         is_selected = radio.is_selected()
    #         if is_selected:
    #             count += 1
    #             assert True
    #         else:
    #             assert False,"dunning没有被选中"
    #     time.sleep(5)

    # Active分页下载功能
    # Dunning分页下载功能
    def user_states_file_download(self):
        time.sleep(2)
        doc = "Active分页下载功能"
        self.wait_elevisble(DunningReportLocator.Dashboards_UseStatus_page, doc)
        self.click_element(DunningReportLocator.UseStatus_Active_download_button, doc)
        # wait = WebDriverWait(driver, 10)
        # wait.until(EC.presence_of_element_located(DunningReportLocator.UseStatus_Active_download_button, doc))
        time.sleep(5)
        self.move_mouse_to_element(DunningReportLocator.UseStatus_Dunning_title)
        self.click_element(DunningReportLocator.UseStatus_Dunning_title, doc)
        self.click_element(DunningReportLocator.UseStatus_Dunning_download_button, doc)
        time.sleep(5)




























